﻿using System.Threading.Tasks;
using Dpx.Models;
using Dpx.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Dpx.ViewModels {
    /// <summary>
    /// 诗词详情页ViewModel。
    /// </summary>
    public class DetailPageViewModel : ViewModelBase {
        /******** 构造函数 ********/

        /// <summary>
        /// 收藏存储。
        /// </summary>
        private readonly IFavoriteStorage favoriteStorage;

        /// <summary>
        /// 诗词详情页ViewModel。
        /// </summary>
        /// <param name="favoriteStorage">收藏存储。</param>
        public DetailPageViewModel(IFavoriteStorage favoriteStorage) {
            this.favoriteStorage = favoriteStorage;
        }

        /******** 绑定属性 ********/

        /// <summary>
        /// 诗词。
        /// </summary>
        public Poetry Poetry {
            get => _poetry;
            set { Set(nameof(Poetry), ref _poetry, value); }
        }

        /// <summary>
        /// 诗词。
        /// </summary>
        private Poetry _poetry;

        /// <summary>
        /// 收藏。
        /// </summary>
        public Favorite Favorite {
            get => _favorite;
            set => Set(nameof(Favorite), ref _favorite, value);
        }

        /// <summary>
        /// 收藏。
        /// </summary>
        private Favorite _favorite;

        /// <summary>
        /// 正在载入。
        /// </summary>
        public bool Loading {
            get => _loading;
            set => Set(nameof(Loading), ref _loading, value);
        }

        /// <summary>
        /// 正在载入。
        /// </summary>
        private bool _loading;

        /******** 绑定命令 ********/

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ??= new RelayCommand(
                async () => await PageAppearingCommandFunction());

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        public async Task PageAppearingCommandFunction() {
            Loading = true;
            var favorite = await favoriteStorage.GetFavoriteAsync(Poetry.Id) ??
                           new Favorite {PoetryId = Poetry.Id};
            isFavorite = favorite.IsFavorite;
            Favorite = favorite;
            Loading = false;
        }

        /// <summary>
        /// 收藏切换命令。
        /// </summary>
        public RelayCommand FavoriteToggledCommand =>
            _favoriteToggledCommand ??= new RelayCommand(async () =>
                await FavoriteToggledCommandFunction());

        /// <summary>
        /// 收藏切换命令。
        /// </summary>
        private RelayCommand _favoriteToggledCommand;

        public async Task FavoriteToggledCommandFunction() {
            if (isFavorite == Favorite.IsFavorite) return;
            isFavorite = Favorite.IsFavorite;
            Loading = true;
            await favoriteStorage.SaveFavoriteAsync(Favorite, true);
            Loading = false;
        }

        /******** 私有变量 ********/
        /// <summary>
        /// 是否为收藏。
        /// </summary>
        public bool isFavorite;
    }
}